<?php 
/**create by fu*/
namespace app\api\validate;

use think\Validate;
use app\lib\exception\ParameterException;
use app\api\validate\BaseValidate;

/**
 * 
 */
class OrderPlace extends BaseValidate
{
	//客户端传过来的products 数组
	protected $oProducts = [
		[
			'product_id' => 1,
			'count'      => 3,
		],
		[
			'product_id' => 2,
			'count'      => 3,
		],
		[
			'product_id' => 3,
			'count'      => 3,
		],
	];
	protected $rule = [
		'products' => 'require|checkProducts',
	];

	//整整protects 数组里面的子项
	protected $singleRule = [
		'product_id' => 'require|isPositiveInteger',
		'count' => 'require|isPositiveInteger',
	];

	protected function checkProducts($values)
	{
		if(!is_array($values))throw new ParameterException(['msg' => '商品参数不正确',]);
		if(empty($values))throw new ParameterException(['msg' => '商品列表不为空',]);
		
		foreach ($values as $key => $value) 
		{
			$res = $this->checkProduct($value); 
			if(!$res)return false;
		}

        return true;
	}

	protected function checkProduct($value)
	{
		//Validate 的构造方法默认传子类的 $rule 数组，当前指定自定义的数组验证
		$validate = new BaseValidate($this->singleRule);

		$result = $validate->check($value);

		// if(!$result)throw new ParameterException(['msg' => '商品列表参数错误']);

	    return $result ? true :false;
		

	}

	protected $message = [
		'products'  =>  'products二维数组参数错误;请检查',
	];
}








 ?>